package com.ws.gold.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ws.gold.domain.RealtimeGoldPrice;
import com.ws.gold.service.RealtimeGoldPriceService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author ws
 * @create 2025/6/23
 */
@RestController
@RequestMapping("/realtime-gold-price")
@RequiredArgsConstructor
public class RealtimeGoldPriceController {

    private final RealtimeGoldPriceService service;

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取最新的黄金价格并入库
     */
    @GetMapping("/getLatestGoldPrice")
    public RealtimeGoldPrice getLatestGoldPrice() {
        return service.getLatestGoldPrice();
    }

    /**
     * 获取图表数据
     */
    @GetMapping("/getChartData")
    public JSONObject getChartData() {
        return service.getChartData();
    }

    /**
     * 修改缓存中的指标值
     */
    @PostMapping("/updateCacheData")
    public Boolean updateCacheData(@RequestBody JSONObject jsonObject) {
        redisTemplate.opsForValue().set("CacheData", jsonObject);
        return true;
    }

    /**
     * 获取缓存中的指标值
     */
    @GetMapping("/getCacheData")
    public JSONObject getCacheData() {
        Object o = redisTemplate.opsForValue().get("CacheData");
        return JSONObject.parseObject(JSON.toJSONString(o));
    }

}
